<template>
  <div class="login">
    <!-- <van-nav-bar>
      <template #left>
        <i class="iconfont f44">&#xe637;</i>
      </template>
    </van-nav-bar> -->
    <navBar @click.native="$router.push('/home/find')"></navBar>

    <div class="content">
      <h2 class="title">您好,请登录</h2>
      <van-form ref="form">
        <van-field
          type="tel"
          placeholder="请输入手机号"
          v-model="form.mobile"
          :rules="rules.mobile"
          name="mobile"
        >
          <template #left-icon>
            <i class="iconfont m15">&#xe60b;</i>
          </template>
        </van-field>
        <van-field
          type="tel"
          placeholder="请输入验证码"
          v-model="form.code"
          :rules="rules.code"
        >
          <template #left-icon>
            <i class="iconfont m15">&#xe60c;</i>
          </template>
          <template #button>
            <div class="code" @click="getcode" v-if="outTime === 6">
              获取验证码
            </div>
            <div class="code" v-else>{{ outTime }}s后重试</div>
          </template>
        </van-field>
      </van-form>
      <div class="tip">
        登录即同意<span class="tip-color">《用户使用协议》</span> 和<span
          class="tip-color"
          >《隐私协议》</span
        >
      </div>
      <van-button type="primary" color="#E40137" round block @click="submit"
        >确定</van-button
      >
    </div>
  </div>
</template>

<script>
// 导入验证码和登录接口
import { auCode, auLogin } from '@/api/login.js'
// 导入保存tuoken
import { setLocal, getLocal } from '@/utile/local.js'
export default {
  data () {
    return {
      outTime: 6,
      form: {
        mobile: '', //	是	string	手机号
        code: '' //	是	string	验证码
      },
      rules: {
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'onChange' },
          {
            validator: value => {
              let reg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              return reg.test(value)
            },
            message: '请输入正确的手机号',
            trigger: 'onChange'
          }
        ],
        code: [{ required: true, message: '请输入验证码', trigger: 'onChange' }]
      }
    }
  },
  created () {
    // 进入页面就要做判断有没有token
    if (getLocal()) {
      this.$router.push('/home/my')
    }
  },
  methods: {
    //  验证码的点击事件  局部验证
    getcode () {
      this.$refs.form
        .validate('mobile')
        .then(() => {
          // 给验证码 设置一个倒计时,一开始就要减减,避免狂点
          this.outTime--
          let _itme = setInterval(() => {
            this.outTime--
            if (this.outTime <= 0) {
              this.outTime = 6
              clearInterval(_itme)
            }
          }, 1000)

          // 调用验证码接口
          auCode({
            mobile: this.form.mobile
          }).then(res => {
            this.$toast.success(res.data.data)
          })
        })
        .catch(() => {
          this.$toast.fail('验证失败')
        })
    },
    // 点击提交 全局验证
    submit () {
      this.$refs.form
        .validate()
        .then(() => {
          // 调用登录接口
          auLogin(this.form).then(res => {
            // console.log(res)
            // 使用stro 的 mutations方法保存用户信息
            this.$store.commit('getUser', res.data.data.user)
            // 保存 token
            setLocal(res.data.data.jwt)

            //修改登录成功的标记
            this.$store.commit('loginStatus', true)
            this.$toast.success('登录通过')

            const _redirect = this.$route.query.redirect
            if (_redirect) {
              this.$router.push(_redirect)
            } else {
              // 跳转到my 页面
              this.$router.push('/home/my')
            }
          })
        })
        .catch(() => {
          this.$toast.fail('登录失败')
        })
    }
  }
}
</script>

<style lang="less" scoped>
.login {
  .content {
    padding: 0 @p15;
    .title {
      font-size: 16px;
      font-family: PingFangSC, PingFangSC-Semibold;
      font-weight: 600;
      text-align: left;
      color: #222222;
      line-height: 25px;
      letter-spacing: 0px;
      padding-top: 50px;
      padding-bottom: 65px;
    }
    .m15 {
      margin-right: 15px;
    }
    .code {
      font-size: 16px;
      font-family: PingFangSC, PingFangSC-Regular;
      font-weight: 400;
      text-align: left;
      color: #00b8d4;
      line-height: 23px;
      letter-spacing: 0px;
    }
    .tip {
      font-size: 12px;
      font-family: PingFangSC, PingFangSC-Regular;
      font-weight: 400;
      text-align: left;
      color: #181a39;
      line-height: 16px;
      letter-spacing: 0px;
      padding-top: 30px;
      padding-bottom: 44px;
      .tip-color {
        color: #00b8d4;
      }
    }
  }
}
</style>
